package com.study.mike.controller;


import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.itextpdf.text.DocumentException;
import com.study.mike.common.annotation.MyLog;
import com.study.mike.common.enums.MethodTypeEnum;
import com.study.mike.model.dto.BookDTO;
import com.study.mike.model.dto.CreateOrderDTO;
import com.study.mike.model.dto.QrDTO;
import com.study.mike.model.entity.Book;
import com.study.mike.model.po.*;
import com.study.mike.service.BookService;
import com.study.mike.result.R;
import com.study.mike.result.RUtil;
import com.study.mike.service.ResourceService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.core.env.Environment;
import org.springframework.core.task.TaskExecutor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author KangHui
 * @since 2022-07-08
 */
@RestController
@RequestMapping("/common/book")
public class BookController {
    @Resource
    private Environment environment;


    @Resource
    private BookService bookService;

    @Resource
    private ResourceService resourceService;

    @Resource
    private RedissonClient redissonClient;

    @Resource(name = "threadPool")
    private TaskExecutor taskExecutor;

    /**
     * 新增书籍
     *
     * @param book
     * @return
     */
    @PostMapping("/addBook")
    @MyLog(title = "新增", methodType = MethodTypeEnum.INSERT)
    public R<BookDTO> addBook(@RequestBody @Validated Book book) {
        environment.getProperty("name");
        BookDTO bookInfo = bookService.addBook(book);
        if (ObjectUtil.isNotNull(bookInfo)) {
            return RUtil.success(book);
        }
        return RUtil.fail("新增失败！");
    }

    /**
     * 编辑书籍
     *
     * @param book
     * @return
     */
    @PostMapping("/editBook")
    public R<BookDTO> editBook(@RequestBody @Validated UpdateBookPO book) {
        BookDTO bookInfo = bookService.editBook(book);
        if (ObjectUtil.isNotNull(bookInfo)) {
            return RUtil.success(book);
        }
        return RUtil.fail("编辑失败！");
    }

    /**
     * 删除书籍
     *
     * @param bookPO
     * @return
     */
    @PostMapping("/deleteBook")
    public R deleteBook(@RequestBody @Validated DeleteBookPO bookPO) {
        RLock lock = redissonClient.getLock("mike:test:lock" + bookPO.getIds());
        try {
            lock.lock();
            Boolean flag = bookService.deleteBook(bookPO);
            if (flag) {
                return RUtil.success(true);
            }
            return RUtil.fail("删除失败！");
        } finally {
            lock.unlock();
        }

    }

    /**
     * 分页获取书籍信息
     *
     * @param bookPO
     * @return
     */
    @PostMapping("/queryBook")
    @MyLog(title = "查询", methodType = MethodTypeEnum.SELECT)
    public R<Page<BookDTO>> queryBook(@RequestBody @Validated QueryBookPO bookPO) {
        return RUtil.success(bookService.queryBook(bookPO));
    }

    /**
     * 设置书籍状态
     *
     * @param bookPO
     * @return
     */
    @PostMapping("/setStatus")
    public R setStatus(@RequestBody @Validated SetBookStatusPO bookPO) {
        return RUtil.success(bookService.setStatus(bookPO));
    }

    /**
     * 导入书籍
     *
     * @return
     */
    @PostMapping("/importBook")
    public R importBook() throws Exception {
        Boolean flag = bookService.importBook();
        if (flag) {
            return RUtil.success(true);
        }
        return RUtil.fail("导入失败！");
    }

    /**
     * 导出书籍
     */
    @PostMapping("/exportBook")
    public R exportBook() throws IOException {
        Boolean flag = bookService.exportBook();
        if (flag) {
            return RUtil.success(true);
        }
        return RUtil.fail("导出失败！");
    }

    /**
     * 下载书籍信息
     *
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    @GetMapping("/downloadBook")
    public R downloadBook() throws IOException, DocumentException {
        return RUtil.success(bookService.downloadBook());
    }

    /**
     * 获取资源树
     *
     * @return
     */
    @PostMapping("/treeResource")
    public R treeResource() {
        return RUtil.success(resourceService.resourceList());
    }

    /**
     * 获取配置资源
     */
    @PostMapping("/getResource")
    public R getResource() {
        System.out.println(environment.getProperty("name"));
        return RUtil.success();
    }

    /**
     * 新增/修改书籍信息
     *
     * @param book
     * @return
     */
    @PostMapping("/saveOrUpdateBook")
    public R saveOrUpdateBook(@RequestBody Book book) {
        boolean flag = bookService.saveOrUpdateBook(book);
        return RUtil.success(flag);
    }

    /**
     * 批量存储书籍信息
     *
     * @return
     */
    @PostMapping("/saveBatchBook")
    public R saveBatchBook() {
        bookService.saveBatchBook();
        return RUtil.success(true);
    }

    /**
     * 同步数据到缓存
     *
     * @return
     */
    @PostMapping("/synchronizationData")
    public R synchronizationData() {
        bookService.synchronizationData();
        return RUtil.success();
    }

    /**
     * 获取缓存数据
     *
     * @return
     */
    @PostMapping("/redisBookInfo")
    public R<Book> redisBookInfo() throws JsonProcessingException {
        List<Book> bookList = bookService.redisBookInfo();
        if (ObjectUtil.isEmpty(bookList)) {
            return RUtil.fail("缓存数据为空！");
        }
        return RUtil.success(bookList);
    }

    /**
     * 生成支付二维码
     *
     * @param createOrderDTO
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("/createQr")
    public R<QrDTO> createQr(@RequestBody CreateOrderDTO createOrderDTO) throws JsonProcessingException {
        QrDTO qrDTO = bookService.createQr(createOrderDTO);
        if (ObjectUtil.isNull(qrDTO.getPayCode())) {
            return RUtil.fail("操作失败！");
        }
        return RUtil.success(qrDTO);
    }

    @PostMapping("/test")
    public R test() {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Book::getBookName, "大数据应用管理");
        Book book = bookService.getOne(wrapper);
        try {

        } catch (Exception e) {
            Console.log("数据异常");
        }
        return RUtil.success(book);
    }
}

